package com.hudongwx.sadpw.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class TieZiDetailActivity extends AppCompatActivity {
    RecyclerView recyclerView1;
    RecyclerView recyclerView2;
    MyRecycleAdapterTuiJian myRecycleAdapterTuiJian;
    MyRecycleAdapterTuiJian1 myRecycleAdapterTuiJian1;
    JSONObject jobj;
    JSONArray jsonArray;
    String TAG;
    int type;
    Intent intent;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tie_zi_detail);
        Log.i(TAG, "onCreate: recyclerView1>>>"+(null==recyclerView1));
        recyclerView1= (RecyclerView) findViewById(R.id.tiezi_rec_1);
        recyclerView2= (RecyclerView) findViewById(R.id.tiezi_rec_2);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView1.setLayoutManager(gridLayoutManager);
        myRecycleAdapterTuiJian=new MyRecycleAdapterTuiJian(jobj);
        recyclerView1.setAdapter(myRecycleAdapterTuiJian);

        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 1);
        recyclerView2.setLayoutManager(gridLayoutManager1);
        myRecycleAdapterTuiJian1=new MyRecycleAdapterTuiJian1(jsonArray);
        recyclerView2.setAdapter(myRecycleAdapterTuiJian1);
        imageView= (ImageView) findViewById(R.id.tiezi_back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        parseShan1();
        parseShan();

    }


    //recycleAdapter
    class MyRecycleAdapterTuiJian extends RecyclerView.Adapter<MyRecycleViewHolderTuiJian>{
        JSONObject jobj;

        public MyRecycleAdapterTuiJian(JSONObject jobj) {
            this.jobj = jobj;
        }
        public void updateC2(JSONObject jobj){
            this.jobj=jobj;
            notifyDataSetChanged();
        }
//
//                public MyRecycleAdapterTuiJian(JSONArray jsonArray1) {
//            this.jsonArray = jsonArray1;
//        }
//        public void updateC2(JSONArray jasonArray1){
//            this.jsonArray=jasonArray1;
//            notifyDataSetChanged();
//        }
        @Override
        public MyRecycleViewHolderTuiJian onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.tiezi_1, null);
            return new MyRecycleViewHolderTuiJian(view);
        }

        @Override
        public void onBindViewHolder(MyRecycleViewHolderTuiJian holder, int position) {
            if (null==jobj){
                recyclerView1.setVisibility(View.GONE);
            }else {
                try {
                    x.image().bind(holder.imageView1,jobj.getString("UserIcon"));
                    holder.textView1.setText(jobj.getString("UserName"));
                    holder.textView2.setText(jobj.getString("DateTime"));
                    holder.textView4.setText(jobj.getString("Title"));
                    holder.textView5.setText(jobj.getString("Content"));
                    if (jobj.getString("CommentCount").equals("")){
                        holder.textView6.setText("0");
                    }else {
                        holder.textView6.setText(jobj.getString("CommentCount"));
                    }
                    JSONArray array = jobj.getJSONArray("ListFile");
                    for (int i = 0; i <array.length() ; i++) {
                        if (array.length()==1){
                            holder.linearLayout2.setVisibility(View.GONE);
                            holder.linearLayout3.setVisibility(View.GONE);
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                        }else if (array.length()==2){
                            holder.linearLayout2.setVisibility(View.GONE);
                            holder.linearLayout3.setVisibility(View.GONE);
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                        }else if (array.length()==3){
                            holder.linearLayout2.setVisibility(View.GONE);
                            holder.linearLayout3.setVisibility(View.GONE);
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                        }else if (array.length()==4){
                            holder.linearLayout3.setVisibility(View.GONE);
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(3).getString("Url"));
                        }else if (array.length()==5){
                            holder.linearLayout3.setVisibility(View.GONE);
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(3).getString("Url"));
                            x.image().bind(holder.imageView6,array.getJSONObject(4).getString("Url"));
                        }else if (array.length()==6){
                            holder.linearLayout3.setVisibility(View.GONE);
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(3).getString("Url"));
                            x.image().bind(holder.imageView6,array.getJSONObject(4).getString("Url"));
                            x.image().bind(holder.imageView7,array.getJSONObject(5).getString("Url"));
                        }else if (array.length()==7){
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(3).getString("Url"));
                            x.image().bind(holder.imageView6,array.getJSONObject(4).getString("Url"));
                            x.image().bind(holder.imageView7,array.getJSONObject(5).getString("Url"));
                            x.image().bind(holder.imageView8,array.getJSONObject(6).getString("Url"));
                        }else if (array.length()==8){
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(3).getString("Url"));
                            x.image().bind(holder.imageView6,array.getJSONObject(4).getString("Url"));
                            x.image().bind(holder.imageView7,array.getJSONObject(5).getString("Url"));
                            x.image().bind(holder.imageView8,array.getJSONObject(6).getString("Url"));
                            x.image().bind(holder.imageView9,array.getJSONObject(7).getString("Url"));
                        }else if (array.length()>=9){
                            x.image().bind(holder.imageView2,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView3,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(2).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(3).getString("Url"));
                            x.image().bind(holder.imageView6,array.getJSONObject(4).getString("Url"));
                            x.image().bind(holder.imageView7,array.getJSONObject(5).getString("Url"));
                            x.image().bind(holder.imageView8,array.getJSONObject(6).getString("Url"));
                            x.image().bind(holder.imageView9,array.getJSONObject(7).getString("Url"));
                            x.image().bind(holder.imageView10,array.getJSONObject(8).getString("Url"));
                        }else if (array.length()<=0){
                            holder.linearLayout1.setVisibility(View.GONE);
                            holder.linearLayout2.setVisibility(View.GONE);
                            holder.linearLayout3.setVisibility(View.GONE);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if (null==jobj)return 0;
            return 1;
        }
    }
    class MyRecycleViewHolderTuiJian extends RecyclerView.ViewHolder {
        TextView textView1,textView2,textView3,textView4,textView5,textView6;
        ImageView imageView1,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9,imageView10;
        LinearLayout linearLayout1,linearLayout2,linearLayout3;
        public MyRecycleViewHolderTuiJian(View itemView) {
            super(itemView);
            textView1= (TextView) itemView.findViewById(R.id.xiangqing_tv_1);
            textView2= (TextView) itemView.findViewById(R.id.xiangqing_tv_2);
            textView3= (TextView) itemView.findViewById(R.id.xiangqing_tv_3);
            textView4= (TextView) itemView.findViewById(R.id.xiangqing_tv_4);
            textView5= (TextView) itemView.findViewById(R.id.xiangqing_tv_5);
            textView6= (TextView) itemView.findViewById(R.id.tiezi_huitie_tv);
            imageView1= (ImageView) itemView.findViewById(R.id.xingqing_iv_1);
            imageView2= (ImageView) itemView.findViewById(R.id.ll_iv_1);
            imageView3= (ImageView) itemView.findViewById(R.id.ll_iv_2);
            imageView4= (ImageView) itemView.findViewById(R.id.ll_iv_3);
            imageView5= (ImageView) itemView.findViewById(R.id.ll_iv_4);
            imageView6= (ImageView) itemView.findViewById(R.id.ll_iv_5);
            imageView7= (ImageView) itemView.findViewById(R.id.ll_iv_6);
            imageView8= (ImageView) itemView.findViewById(R.id.ll_iv_7);
            imageView9= (ImageView) itemView.findViewById(R.id.ll_iv_8);
            imageView10= (ImageView) itemView.findViewById(R.id.ll_iv_9);
            linearLayout1= (LinearLayout) itemView.findViewById(R.id.xiangqing_ll1);
            linearLayout2= (LinearLayout) itemView.findViewById(R.id.xiangqing_ll2);
            linearLayout3= (LinearLayout) itemView.findViewById(R.id.xiangqing_ll3);
        }
    }
    //解析靓贴
    public void parseShan(){
        intent = getIntent();
        type = intent.getIntExtra("type", 0);
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Posts?postId="+type);
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONObject data = result.getJSONObject("Data");
                    Log.i(TAG, "onSuccess: data___detail:"+data.toString());
                    Log.i(TAG, "onSuccess: type++++++"+type);
                    myRecycleAdapterTuiJian.updateC2(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: >>>>>>>>>>>>>");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }



    //recycleAdapter1111111
    class MyRecycleAdapterTuiJian1 extends RecyclerView.Adapter<MyRecycleViewHolderTuiJian1>{
        JSONArray jsonArray;
//        public MyRecycleAdapterTuiJian1(JSONObject jobj) {
//            this.jobj = jobj;
//        }
//        public void updateC2(JSONObject jobj){
//            this.jobj=jobj;
//            notifyDataSetChanged();
//        }
        //
                public MyRecycleAdapterTuiJian1(JSONArray jsonArray1) {
            this.jsonArray = jsonArray1;
        }
        public void updateC2(JSONArray jasonArray1){
            this.jsonArray=jasonArray1;
            notifyDataSetChanged();
        }
        @Override
        public MyRecycleViewHolderTuiJian1 onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.tiezi_2, null);
            return new MyRecycleViewHolderTuiJian1(view);
        }


        @Override
        public void onBindViewHolder(MyRecycleViewHolderTuiJian1 holder, int position) {
            if (null==jsonArray){
                recyclerView1.setVisibility(View.GONE);
            }else {
                try {
                    int i;
                    x.image().bind(holder.imageView11,jsonArray.getJSONObject(position).getString("UserIcon"));
                    holder.textView21.setText(jsonArray.getJSONObject(position).getString("UserName"));
                    holder.textView31.setText(jsonArray.getJSONObject(position).getString("DateTime"));
                    holder.textView41.setText(jsonArray.getJSONObject(position).getString("Floor"));
                    holder.textView51.setText(jsonArray.getJSONObject(position).getString("Content"));
                    JSONArray sonComment = jsonArray.getJSONObject(position).getJSONArray("SonComment");
                    Log.i(TAG, "onBindViewHolder: ssssss"+sonComment.length());
                    for ( i = 0; i < sonComment.length(); i++) {
                        String string = sonComment.getJSONObject(i).getString("Content");
                        TextView textView=new TextView(TieZiDetailActivity.this);
                        textView.setText(string);
                        holder.linearLayout.addView(textView);
                        i=sonComment.length();
                    }
//                    tv.setText(Integer.toString((jsonArray.length()+i)));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if (null==jsonArray)return 0;
            return jsonArray.length();
        }
    }
    class MyRecycleViewHolderTuiJian1 extends RecyclerView.ViewHolder {
        TextView textView21,textView31,textView41,textView51;
        ImageView imageView11;
        LinearLayout linearLayout;
        public MyRecycleViewHolderTuiJian1(View itemView) {
            super(itemView);
            textView21= (TextView) itemView.findViewById(R.id.huitie_tv_1);
            textView31= (TextView) itemView.findViewById(R.id.huitie_tv_2);
            textView41= (TextView) itemView.findViewById(R.id.huitie_tv_3);
            textView51= (TextView) itemView.findViewById(R.id.huitie_tv_4);
            linearLayout= (LinearLayout) itemView.findViewById(R.id.huitie_ll);
            imageView11= (ImageView) itemView.findViewById(R.id.huitie_iv_1);
        }
    }
    //解析靓贴
    public void parseShan1(){
        intent = getIntent();
        type = intent.getIntExtra("type", 0);
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Comment?postId="+type+"&pageSize=10&pageIndex=1");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: data___detail:"+data.toString());
                    Log.i(TAG, "onSuccess: type++++++"+type);
                    myRecycleAdapterTuiJian1.updateC2(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: >>>>>>>>>>>>>");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
